package org.news.web.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.news.web.domain.vo.CommentsVo;
import org.news.web.domain.bo.CommentsBo;
import org.news.web.service.ICommentsService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 评论
 *
 * @author Gu Ci
 * @date 2024-04-03
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/web/comments")
public class CommentsController extends BaseController {

    private final ICommentsService commentsService;

    /**
     * 查询评论列表
     */
    @SaCheckPermission("web:comments:list")
    @GetMapping("/list")
    public TableDataInfo<CommentsVo> list(CommentsBo bo, PageQuery pageQuery) {
        return commentsService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出评论列表
     */
    @SaCheckPermission("web:comments:export")
    @Log(title = "评论", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CommentsBo bo, HttpServletResponse response) {
        List<CommentsVo> list = commentsService.queryList(bo);
        ExcelUtil.exportExcel(list, "评论", CommentsVo.class, response);
    }

    /**
     * 获取评论详细信息
     *
     * @param commentId 主键
     */
    @SaCheckPermission("web:comments:query")
    @GetMapping("/{commentId}")
    public R<CommentsVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long commentId) {
        return R.ok(commentsService.queryById(commentId));
    }

    /**
     * 新增评论
     */
    @SaCheckPermission("web:comments:add")
    @Log(title = "评论", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CommentsBo bo) {
        return toAjax(commentsService.insertByBo(bo));
    }

    /**
     * 修改评论
     */
    @SaCheckPermission("web:comments:edit")
    @Log(title = "评论", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CommentsBo bo) {
        return toAjax(commentsService.updateByBo(bo));
    }

    /**
     * 删除评论
     *
     * @param commentIds 主键串
     */
    @SaCheckPermission("web:comments:remove")
    @Log(title = "评论", businessType = BusinessType.DELETE)
    @DeleteMapping("/{commentIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] commentIds) {
        return toAjax(commentsService.deleteWithValidByIds(List.of(commentIds), true));
    }
}
